package cqut.gbq.qimo.controller;

import cqut.gbq.qimo.entity.LoginUser;
import cqut.gbq.qimo.entity.ResBean;
import cqut.gbq.qimo.entity.Student;
import cqut.gbq.qimo.entity.Teacher;
import cqut.gbq.qimo.service.StudentsService;
import cqut.gbq.qimo.service.TeacherService;
import cqut.gbq.qimo.utils.MD5Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;

/**
 * @ClassName LoginController
 * @Description TODO
 * @Author guobenqi
 * @Date 2021/6/17 10:04
 */

@Api(tags = "登陆相关")
@RestController

public class LoginController {
    @Autowired
    private StudentsService studentsService;
    @Autowired
    private TeacherService teacherService;


    @ApiOperation("登录相关")
    @PostMapping("/login")
    public ResBean login(@RequestBody LoginUser loginUser, HttpServletRequest request){
        System.out.println(loginUser);

        String password = MD5Utils.MD5(loginUser.getPassword());
        if (loginUser.getType().equals("teacher")){

            Teacher teacher = teacherService.login(loginUser.getUsername(),password);
            if (teacher != null){
                return ResBean.success("登录成功",teacher);
            }



        }else{
            Student student = studentsService.login(loginUser.getUsername(),password);
            System.out.println(student);
            if (student != null){
                return ResBean.success("登录成功",student);
            }
        }
        return ResBean.error("登录失败");
    }
}
